<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-logo">
        <a href="#">
          <img src="../assets/logo.jpg" alt="logo">
        </a>
      </div>
      <div class="login-form">
        <h3>求职进程智管平台</h3>
        <el-form ref="form" :model="loginForm" :rules="rules" label-position="left" label-width="0">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="do_login" :loading="loading">登录</el-button>
            <el-button @click="showRegisterDialog">注册</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>

    <!-- 注册对话框 -->
    <el-dialog :visible.sync="registerDialogVisible" title="注册新用户" width="50%">
      <el-form ref="registerForm" :model="registerForm" :rules="registerRules" label-position="left" label-width="0">
        <el-form-item prop="username">
          <el-input v-model="registerForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="registerForm.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input type="password" v-model="registerForm.confirmPassword" placeholder="请确认密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="do_register" :loading="loading">注册</el-button>
          <el-button @click="registerDialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { login, register } from '@/api/login.js'
import { setToken } from '@/utils/auth'

export default {
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      registerForm: {
        username: '',
        password: '',
        confirmPassword: ''
      },
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      registerRules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (value !== this.registerForm.password) {
                callback(new Error('两次输入的密码不一致'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ]
      },
      loading: false,
      registerDialogVisible: false
    }
  },
  methods: {
    do_login() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          login(this.loginForm.username, this.loginForm.password).then(res => {
            setToken(res.data.accessToken)
            this.$router.push({ path: '/' })
          }).catch(() => {
            this.$message.error('用户名或密码错误')
            this.loading = false
          })
        }
      })
    },
    showRegisterDialog() {
      this.registerDialogVisible = true
    },
    do_register() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true
          register(this.registerForm.username, this.registerForm.password).then(() => {
            this.$message.success('注册成功')
            this.registerDialogVisible = false
            this.loading = false
          }).catch(() => {
            this.$message.error('注册失败')
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

<style lang="scss">
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: url('../assets/background.jpg') no-repeat center center;
  background-size: cover;
}

.login-box {
  background-color: rgba(255, 255, 255, 0.9);
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  text-align: center;
}

.login-logo img {
  width: 120px;
  margin-bottom: 5px;
  border-radius: 30px;
  border: 3px;
}

.login-form h3 {
  width: 50vh;
  margin-bottom: 20px;
}
</style>
